; paging.s
; setup paging
; liuw <liuw@liuw.name>
; 10-Jun-08

; we assume that we have 32MB of RAM
; linear mapping

; start from address 0x10:PageDirBase 
PageDirBase	equ	00000000h
PageTableBase	equ	PageDirBase+4096; PageDirBase+4K, reserved for extension
PDEs		equ	8 ; 32M = 4M * 8 
KERNDATASEG	equ	10h

[GLOBAL init_paging]
align	32
init_paging:
	mov	eax, KERNDATASEG
	mov	es, eax
	; set up page directory entries
	mov 	ecx, PDEs
	mov	edi, PageDirBase
	xor	eax, eax
	mov	eax, PageTableBase+7 ; set u/s r/w present bit
	cld
.1:
	stosd
	add	eax, 1000h
	loop	.1

	; set up page table entries
	mov	ecx, PDEs * 1024
	mov	edi, PageTableBase
	xor	eax, eax
	mov	eax, 7		; set u/s r/w present bit
	cld
.2
	stosd
	add	eax, 1000h
	loop	.2

	; set up control registers
	mov	eax, PageDirBase
	mov	cr3, eax
	mov 	eax, cr0
	or	eax, 80000000h	; set PG bit
	mov	cr0, eax
	ret
